package com.aiwu.memory;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import com.aiwu.library.util.DisplayUtil;

import org.citra.citra_emu.R;

public class MemoryFirstFragment extends Fragment implements OnActionCallback {
    private static final String TAG = MemoryFirstFragment.class.getName();
    private EditText etNum;
    private ViewGroup layoutBottom;
    private CheckBox cbDepthSearch;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_memory_first, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        MemoryManager.getInstance().addOnActionCallback(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        MemoryManager.getInstance().removeOnActionCallback(this);
    }

    private void initView(View view) {
        etNum = view.findViewById(R.id.et_num);
        layoutBottom = view.findViewById(R.id.layout_bottom);
        view.findViewById(R.id.btn_search_type).setOnClickListener(v -> {
            new SearchTypeDialog(getContext()).show();
        });
        view.findViewById(R.id.iv_tip).setOnClickListener(v -> {
            goTipWeb();
        });
        MemoryManager.disableShowInput(etNum, true);

        cbDepthSearch = view.findViewById(R.id.cb_depth_search);
        cbDepthSearch.setChecked(MemoryManager.getInstance().isDepthSearch());
        cbDepthSearch.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (buttonView.isPressed()) {
                MemoryManager.getInstance().enableDepthSearch(isChecked);
            }
        });
    }

    /**
     * 提示网页
     */
    private void goTipWeb() {
        try {
            String url = "https://m.25game.com/BbsTopic/28700";
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(browserIntent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onShowKeyboard() {
        if (getContext() != null) {
            if (DisplayUtil.isPortrait()) {
                layoutBottom.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.bg_dialog));
            } else {
                layoutBottom.setBackgroundResource(R.drawable.dialog_bottom_left);
            }
        }
    }

    @Override
    public void onCloseKeyboard() {
        if (getContext() != null) {
            layoutBottom.setBackgroundResource(R.drawable.dialog_bottom);
        }
    }

}
